package com.liumin.comlink.http.exception;

import android.net.ParseException;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import javax.net.ssl.SSLException;

import retrofit2.HttpException;

/**
 * 网路请求异常
 * 2019/12/25 10:25
 *
 * @author LiuWeiHao
 */
public class ExceptionHandle {
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;
    //无法使用请求的内容特性来响应请求的网页
    private static final int FAIL_QUEST = 406;
    private static final int BAD_REQUEST = 400;
    /**
     * 服务错误
     */
    private static final int ERROR_SERVICE = 600;


    public static ApiException handleException(Throwable e) {
        ApiException ex;
        if (e == null) {
            ex = new ApiException(Error.UNKNOWN, e);
        } else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ApiException(httpException.code(), httpException.message(), e);
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ex = new ApiException(Error.PARSE_ERROR, e);
        } else if (e instanceof ConnectException) {
            ex = new ApiException(Error.NETWORD_ERROR, e);
        } else if (e instanceof SSLException) {
            ex = new ApiException(Error.SSL_ERROR, e);
        } else if (e instanceof java.net.SocketTimeoutException
                || e instanceof UnknownHostException
                || e instanceof TimeoutException
                || e instanceof SocketException) {
            ex = new ApiException(Error.TIMEOUT_ERROR, e);
        } else if (e instanceof java.io.EOFException
                || e instanceof NullPointerException) {
            ex = new ApiException(Error.EOF_ERROR, e);
        } else {
            ex = new ApiException(Error.UNKNOWN, e);
        }
        if (e == null) {
            ex.setMessage("未定义错误");
        }
        return ex;
    }

    /**
     * 自定义错误
     */
    public class Error {
        /**
         * 未知错误
         */
        static final int UNKNOWN = 1000;
        /**
         * 解析错误
         */
        static final int PARSE_ERROR = 1001;
        /**
         * 解析no content错误
         */
        static final int EOF_ERROR = 1007;
        /**
         * 网络错误
         */
        static final int NETWORD_ERROR = 1002;
        /**
         * 协议出错
         */
        static final int HTTP_ERROR = 1003;

        /**
         * 证书出错
         */
        static final int SSL_ERROR = 1005;

        /**
         * 连接超时
         */
        static final int TIMEOUT_ERROR = 1006;

        public static final int LOGIN_ERROR = -1000;
        public static final int DATA_EMPTY = -2000;


    }

}


